//********************************************************************************************************
// Product Name: MapWindow.dll Alpha
// Description:  The basic module for MapWindow version 6.0
//********************************************************************************************************
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at 
// http://www.mozilla.org/MPL/ 
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
// ANY KIND, either expressed or implied. See the License for the specificlanguage governing rights and 
// limitations under the License. 
//
// The Original Code is from MapWindow.dll version 6.0
//
// The Initial Developer of this Original Code is Ted Dunsford. Created 6/25/2008 11:27:09 AM
// 
// Contributor(s): (Open source contributors should list themselves and their modifications here). 
//
//********************************************************************************************************

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

using MapWindow.Drawing;
using MapWindow.Geometries;

namespace MapWindow.DirectX
{


    /// <summary>
    /// TexturedWedgeList
    /// </summary>
    public class ColoredWedgeListDX : PrimitiveBaseDX
    {
        #region Private Variables




        float _width;

        

        #endregion

        #region Constructors

       

        /// <summary>
        /// Creates a new instance of TexturedWedgeList, and specifies the symbolizer to use
        /// </summary>
        /// <param name="color">The color to make the list</param>
        /// <param name="width">The floating point width of the line, which determines how to draw the wedges</param>
        public ColoredWedgeListDX(Color color, float width):base(color)
        {
            
         
            _width = width;
        
            
        }

        #endregion

        #region Methods


       
        /// <summary>
        /// Adds a new wedge to the list
        /// </summary>
        /// <param name="start">The start point for the segment before the wedge</param>
        /// <param name="center">The center point where the wedge will be inserted</param>
        /// <param name="end">The end point for the segment after the wedge</param>
        public void AddWedge(Coordinate start, Coordinate center, Coordinate end)
        {

            double mZ = center.Z;
            if (double.IsNaN(mZ)) mZ = 0.0;
            double sZ = start.Z;
            if (double.IsNaN(sZ)) sZ = 0.0;
            double eZ = end.Z;
            if (double.IsNaN(eZ)) eZ = 0.0;



            Vector vCenter = new Vector(center.X, center.Y, mZ);
            Vector vStart = new Vector(start.X, start.Y, sZ);
            Vector vEnd = new Vector(end.X, end.Y, eZ);


            Vector leftStart = Perpendicular(vStart, vCenter);
            Vector leftEnd = Perpendicular(vCenter, vEnd);
            Vector dirStart = vCenter.Subtract(vStart);

            Vertices.AddCoordinate(vCenter);

            if (leftEnd.Dot(dirStart) < 0)
            {
                // The left side of this joint is squashed, so create the wedge from the other side
                leftStart = leftStart.Multiply(_width / 2);
                Vector vWedge2 = vCenter.Subtract(leftStart);
                //  tu = .5F - Math.Abs(leftStart.X); // This ensures the border width is correct and that the texture isn't scaled down to the size of the wedge.
                Vertices.AddCoordinate(vWedge2);

                leftEnd = leftEnd.Multiply(_width / 2);
                Vector vWedge3 = vCenter.Subtract(leftEnd);
                //  tu = .5F + Math.Abs(leftEnd.X);
                Vertices.AddCoordinate(vWedge3);
            }
            else
            {
                leftStart = leftStart.Multiply(_width / 2);
                Vector vWedge2 = vCenter.Add(leftStart);
                // tu = .5f - Math.Abs(leftStart.X); // This ensures the border width is correct and that the texture isn't scaled down to the size of the wedge.
                Vertices.AddCoordinate(vWedge2);

                leftEnd = leftEnd.Multiply(_width / 2);
                Vector vWedge3 = vCenter.Add(leftEnd);
                //tu = .5f + Math.Abs(leftEnd.X);
                Vertices.AddCoordinate(vWedge3);
            }



            //float mZ = Convert.ToSingle(center.Z);
            //if (float.IsNaN(mZ)) mZ = 0F;
            //float sZ = Convert.ToSingle(start.Z);
            //if (float.IsNaN(sZ)) sZ = 0F;
            //float eZ = Convert.ToSingle(end.Z);
            //if (float.IsNaN(eZ)) eZ = 0F;
            

           
            //Vector3 vCenter = new Vector3(Convert.ToSingle(center.X), Convert.ToSingle(center.Y), mZ);
            //Vector3 vStart = new Vector3(Convert.ToSingle(start.X), Convert.ToSingle(start.Y), sZ);
            //Vector3 vEnd = new Vector3(Convert.ToSingle(end.X), Convert.ToSingle(end.Y), eZ);
 

            //Vector3 leftStart = Perpendicular(vStart, vCenter);
            //Vector3 leftEnd = Perpendicular(vCenter, vEnd);
            //Vector3 dirStart = Vector3.Subtract(vCenter, vStart);
            
            //Vertices.AddCoordinate(vCenter);

            //if (Vector3.Dot(leftEnd, dirStart) < 0)
            //{
            //    // The left side of this joint is squashed, so create the wedge from the other side
            //    leftStart.Multiply(_width / 2);
            //    Vector3 vWedge2 = Vector3.Subtract(vCenter, leftStart);
            //  //  tu = .5F - Math.Abs(leftStart.X); // This ensures the border width is correct and that the texture isn't scaled down to the size of the wedge.
            //    Vertices.AddCoordinate(vWedge2);

            //    leftEnd.Multiply(_width / 2);
            //    Vector3 vWedge3 = Vector3.Subtract(vCenter, leftEnd);
            //  //  tu = .5F + Math.Abs(leftEnd.X);
            //    Vertices.AddCoordinate(vWedge3);
            //}
            //else
            //{
            //    leftStart.Multiply(_width/2);
            //    Vector3 vWedge2 = Vector3.Add(vCenter, leftStart);
            //   // tu = .5f - Math.Abs(leftStart.X); // This ensures the border width is correct and that the texture isn't scaled down to the size of the wedge.
            //    Vertices.AddCoordinate(vWedge2);

            //    leftEnd.Multiply(_width / 2);
            //    Vector3 vWedge3 = Vector3.Add(vCenter, leftEnd);
            //    //tu = .5f + Math.Abs(leftEnd.X);
            //    Vertices.AddCoordinate(vWedge3);
            //}

           

        }

        private Vector Perpendicular(Vector start, Vector end)
        {
            Vector up = new Vector(0, 0, 1);
            Vector dir = end.Subtract(start);
            //Vector perp = up.Cross(dir);
            Vector perp = dir.Cross(up);
            perp.Normalize();
            return perp;
        }


      


        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the width of the line so that the wedge size can be calculated
        /// </summary>
        public float Width
        {
            get { return _width; }
            set { _width = value; }
        }

       

        #endregion

       
    }
}
